﻿using System;
using System.IO;

namespace CarolLib.WinService
{
    [Obsolete]
    public class FileLogger : ILogger
    {
        #region Private Methods

        private static object obj = new object();

        private void WriteToFile(string filename, string content)
        {
            string dir = Path.GetDirectoryName(filename);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            using (StreamWriter sw = new StreamWriter(filename, true))
            {
                sw.WriteLine(content);
                sw.Close();
            }
        }

        #endregion

        #region ILog Members

        public string ServiceDirectory { get; set; }

        public string GetDateTime()
        {
            return "[" + DateTime.Now.ToString("HH:mm:ss") + "] ";
        }

        public void Output(string msg)
        {
            lock (obj)
            {
                Console.WriteLine(GetDateTime() + msg);
            }
        }

        public void Output(string msg, bool logToFile)
        {
            lock (obj)
            {
                Console.WriteLine(GetDateTime() + msg);

                if (logToFile)
                {
                    this.WriteToFile(ServiceDirectory + @"\Log\log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt", GetDateTime() + msg);
                }
            }
        }

        public void Output(string msg, ConsoleColor color)
        {
            lock (obj)
            {
                Console.ForegroundColor = color;
                Console.WriteLine(GetDateTime() + msg);
                Console.ResetColor();
            }
        }

        public void Output(string msg, ConsoleColor color, bool logToFile)
        {
            lock (obj)
            {

                Console.ForegroundColor = color;
                Console.WriteLine(GetDateTime() + msg);
                Console.ResetColor();

                if (logToFile)
                {
                    this.WriteToFile(ServiceDirectory + @"\Log\log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt", GetDateTime() + msg);
                }
            }
        }

        #endregion
    }
}
